/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import biz.LoginProgess;
import biz.ReplyProgess;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Account;
import entities.Comment;
import entities.Complain;
import entities.Reply;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author THOHCGC00177
 */
@ManagedBean
@SessionScoped
public class ComplainProgess {

    @EJB
    private biz.CommentProgess commentProgess;
    /**
     *
     * Call EJB
     */
    @EJB
    private ReplyProgess replyProgess;
    @EJB
    private LoginProgess loginProgess;
    @EJB
    private biz.ComplainProgess complainProgess;
    private List<Complain> listComplain;
    private String title;
    private String content;
    private String msg;
    private boolean check;
    private Complain ComplainItem;
    private Complain ComplainDetail;
    private Complain ComplainItemAdmin;
    private List<Complain> listAllComplain;
    private Reply lstReply;
    private String temp;
    private List<Comment> lstCmt;
    private Complain cm;

    /**
     *
     * Function create Complain
     */
    public void createListComplain() {
        listComplain = complainProgess.getComplainForIndex();
    }

    /**
     *
     * Function show list complain
     *
     * @return
     */
    public List<Complain> getListComplain() {
        return listComplain;
    }

    /**
     *
     * Function show complain Detail
     *
     * @param temp is a complain
     * @return
     */
    public String showComplainDetail(Complain temp) {
        ComplainItem = complainProgess.getComplainById(temp.getId());
        lstReply = replyProgess.getReplyById(temp.getId());
        lstCmt = commentProgess.getCommentByComplainId(temp.getId());
        return "complain_detail.xhtml";
    }
    /**
     * show complaint detail  Admin page
     * @param temp
     * @return 
     */
    public String showComplaintDetailAdmin(Complain temp){
        ComplainItem = complainProgess.getComplainById(temp.getId());
        lstReply = replyProgess.getReplyById(temp.getId());
        lstCmt =commentProgess.getCommentByComplainId(temp.getId());
        return "complaint_detail_admin.xhtml";
    }
    public String showComplaintDetailTechnician(Complain temp){
        ComplainItem = complainProgess.getComplainById(temp.getId());
        lstReply = replyProgess.getReplyById(temp.getId());
        lstCmt = commentProgess.getCommentByComplainId(temp.getId());
        return "complaint_detail_technician.xhtml";
    }
    /**
     * 
     * show complaint detail employee page
     * @param temp
     * @return 
     */
    public String showComplainDetailEmployee(Complain temp) {
        ComplainItem = complainProgess.getComplainById(temp.getId());
        lstReply = replyProgess.getReplyById(temp.getId());
        cm = temp;
        return "complain_detail_employee.xhtml";
    }
    /**
     * 
     * function create list comment of a complaint
     */
    public void createLstCmt() {
        lstCmt = commentProgess.getCommentByComplainId(cm.getId());
    }

    public String showComplainDetailAdmin(Complain c) {
        ComplainItemAdmin = complainProgess.getComplainById(c.getId());
        if (c.getProcessing().equals("No")) {
            temp = "a";
        } else {
            temp = null;
        }

        return "admin_relation.xhtml";
    }

    /**
     *
     * Function check clear
     *
     */
    public void checkClear() {
        boolean temp = check;
        if (temp == true) {
            check = false;
            clear();
        }
    }

    public void clear() {
        title = null;
        content = null;

    }

    /**
     *
     * Function create Complain
     *
     * @param name
     * @return
     */
    public String createComplain(String name) {
        //call ejb to get account login
        Account user = loginProgess.getUser(name);
        Date date = new Date();
        //Validate text box
        if ("".equals(title)) {
            msg = "Insert title please!";
            return "";
        }
        if ("".equals(content)) {
            msg = "Insert content please!";
            return "";
        }
        try {
            //Create new complain
            Complain complain = new Complain(1);
            complain.setId(1);
            complain.setAccountId(user);
            complain.setDepartmentId(user.getDepartmentId());
            complain.setTitle(title);
            complain.setContent(content);
            //set status 
            complain.setStatus("New");
            //set processing
            complain.setProcessing("No");
            complain.setDate(date);
            //call ejb to create 
            complainProgess.createComplain(complain);
            msg = "Submit complain success!";
            check = true;
            ComplainDetail = complain;
            checkClear();
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "create_complaint_detail";
    }

    public Complain getComplainDetail() {
        return ComplainDetail;
    }

    public void setComplainDetail(Complain ComplainDetail) {
        this.ComplainDetail = ComplainDetail;
    }

    public void createListAllComplain() {
        listAllComplain = complainProgess.getAllComplain();
    }

    public Reply getLstReply() {
        return lstReply;
    }

    public List<Complain> getListAllComplain() {
        return listAllComplain;
    }

    public Complain getComplainItemAdmin() {
        return ComplainItemAdmin;
    }

    public List<Comment> getLstCmt() {
        return lstCmt;
    }

    public void setLstCmt(List<Comment> lstCmt) {
        this.lstCmt = lstCmt;
    }

    public void setComplainItemAdmin(Complain ComplainItemAdmin) {
        this.ComplainItemAdmin = ComplainItemAdmin;
    }

    public ComplainProgess() {
    }

    public Complain getComplainItem() {
        return ComplainItem;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }
}
